// 餐桌管理功能实现 
#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include "TableManage.h"
// 餐桌信息打印

//定义了打印餐桌信息、餐桌添加等相关函数
// tablePrintPointer: 餐桌节点指针 
void TablePrint(ListPointer tablePrintPointer)
{
	static int tableState = 0;
	char state[4][10] = { "空闲", "维修", "占用", "预定" };
	struct Table *table;
	table = (struct Table *)(tablePrintPointer->listData);
	if (tableState == 0)
	{
		system("cls");
		printf("\n\n\t\t*************** 餐桌信息管理界面 ***************\n\n\n");
		printf("\t\t\t   %-10s%-14s%-12s\n\n", "餐桌号", "状态", "位置");
		tableState = 1;
	}
	printf("\t\t\t   %-10s%-14s%-12s\n", table->tableNo, state[table->tableState], table->tableSeat);
	if (tablePrintPointer->listNext == NULL)
	{
		tableState = 0;
	}
}
// 餐桌添加
// tableData: 餐桌最后一个节点数据指针
void *TableAdd(void *tableData)
{
	struct Table *listTableData;
	struct Table *tempTablePointer;
	listTableData = (struct Table *)malloc(sizeof(struct Table));
	tempTablePointer = (struct Table *)tableData;
	if (tableData == NULL)
	{
		system("cls");
		strcpy(listTableData->tableNo, "100");
	}
	else
	{
		// 类型转换，并加1
		FormatInputString(tempTablePointer->tableNo, '0', '9', 4, 2);
		// 把递增后的编号复制给新编号
		strcpy(listTableData->tableNo, tempTablePointer->tableNo);
	}
	printf("\n\t\t==> 请输入要增加的餐桌状态(0-空闲、1-维修、2-占用)：");
	listTableData->tableState = FormatInputString(NULL, '0', '2', 1, 2);
	printf("\n\t\t==> 请输入要增加的餐桌位置(中文)：");
	FormatInputString(listTableData->tableSeat, 0, 0, 14, 1);
	return listTableData;
}
// 餐桌删除, 返回成功与否标志
// tableRemovePointer: 餐桌节点指针
int TableRemove(ListPointer tableRemovePointer)
{
	static int tableState = 0;
	static char tableNoStr[8] = { 0 };
	struct Table *table;
	if (tableState == 0)
	{
		printf("\n\t\t==> 请输入要删除的餐桌编号(3位)：");
		FormatInputString(tableNoStr, '0', '9', 3, 2);// str为要假删除的编号
		tableState = 1;
	}
	table = (struct Table *)(tableRemovePointer->listData);
	if (strcmp(table->tableNo, tableNoStr) == 0)
	{
		memset(tableNoStr, 0, sizeof(char));
		table->tableState = 1;
		tableState = 0;
		return 1;
	}
	else
	{
		if (tableRemovePointer->listNext == NULL)
		{
			memset(tableNoStr, 0, sizeof(char));
			tableState = 0;
		}
		return 0;
	}
}
// 餐桌数据更新
// tableUpdatePointer: 餐桌节点指针
int TableUpdate(ListPointer tableUpdatePointer)
{
	static int tableState = 0;
	static char tableNoStr[8] = { 0 };
	struct Table *table;
	if (tableState == 0)
	{
		printf("\n\t\t==> 请输入要修改的餐桌编号(3位)：");
		FormatInputString(tableNoStr, '0', '9', 3, 2);
		tableState = 1;
	}
	table = (struct Table *)(tableUpdatePointer->listData);
	if (strcmp(table->tableNo, tableNoStr) == 0)
	{
		memset(tableNoStr, 0, sizeof(char));
		printf("\n\t\t==> 重新输入状态(0-空闲、1-维修、2-占用)：");
		table->tableState = FormatInputString(NULL, '0', '2', 1, 2);
		printf("\n\t\t==> 重新输入位置(中文)：");
		FormatInputString(table->tableSeat, 0, 0, 14, 1);
		tableState = 0;
		return 1;
	}
	else
	{
		if (tableUpdatePointer->listNext == NULL)
		{
			memset(tableNoStr, 0, sizeof(char));
			tableState = 0;
		}
		return 0;
	}
}
// 餐桌查询, 返回成功与否标志
// head: 餐桌链表的头指针
// nState: 餐桌状态
// mState: 餐桌状态
int TableSearch(ListPointer head, int nState, int mState)
{
	ListPointer listPointer, tempListPointer;
	struct Table *data;
	listPointer = LinkedListInit(NULL);
	if (nState > 2)
	{
		system("cls");
		printf("\n\n\t\t*************** 餐桌-查询条件选项 ***************\n\n");
		printf("\t\t\t\t    [1] 全部\n\n");
		printf("\t\t\t\t    [2] 状态");
		printf("\n\n\t\t==> 请输入要查询的条件:");
		if (FormatInputString(NULL, '1', '2', 1, 2) == 1)
		{
			LinkedListPrint(head, TablePrint);
			return 0;
		}
		else
		{
			printf("\n\t\t==> 请输入要查询的餐桌状态(0-空闲、1-维修、2-占用)：");
		}
		nState = FormatInputString(NULL, '0', '2', 1, 2);
	}
	tempListPointer = head->listNext;
	if (tempListPointer == NULL)
	{
		system("cls");
		printf("\n\t\t!!! 未存有餐桌信息，请先添加 !!!\n");
		return 0;
	}
	while (tempListPointer != NULL)
	{
		data = (struct Table *)(tempListPointer->listData);
		if ((data->tableState == nState) || (data->tableState == mState))
		{
			LinkedListAdd(listPointer, data);   // 符合条件的加入新链表中
		}
		tempListPointer = tempListPointer->listNext;
	}
	if (listPointer->listNext != NULL)
	{
		LinkedListPrint(listPointer, TablePrint);   // 打印符合条件的结点
		return 1;
	}
	else
	{
		printf("\n\t\t!!! 未查询到相关信息 !!!");
		return 0;
	}
}
